const app = getApp();
const https = require('../../utils/https.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    disabled: false,
    isOverShare: true,
    canIUseGetUserProfile: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },

  /**
   * 获取用户信息,授权
   */
  getUserInfo(e) {
    this.setData({
      loading: true,
      disabled: true
    })
    if (e.detail.encryptedData) {
      wx.login({
        success: login_res => {
          if (login_res.code) {
            wx.getUserInfo({
              success: info_res => {
                this.Login(login_res.code, info_res)
              }
            })
          } else {
            this.setData({
              loading: false,
              disabled: false
            })
            wx.showToast({
              title: '登录失败',
              icon: 'none',
              duration: 1500
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '授权失败',
        icon: 'none'
      })
      this.setData({
        loading: false,
        disabled: false
      })
    }
  },
  /**
   * 2021.5 新的授权流程
   */
  getUserProfile() {
    const that = this;
    this.setData({
      loading: true,
      disabled: true
    })
    console.log('here')
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: info_res => {
        console.log(info_res)
        wx.login({
          success: login_res => {
            if (login_res.code) {
              this.Login(login_res.code, info_res)
            } else {
              that.setData({
                loading: false,
                disabled: false
              })
              wx.showToast({
                title: '登录失败',
                icon: 'none',
                duration: 1500
              })
            }
          }
        })
      },
      fail: () => {
        wx.showToast({
          title: '授权失败',
          icon: 'none'
        })
        this.setData({
          loading: false,
          disabled: false
        })
      }
    })

  },
  /**
   * 登录
   */
  Login(code, info_res) {
    console.log(info_res)
    wx.getSetting({
      success: set_res => {
        if (set_res.authSetting['scope.userInfo']) {
          https.post('login', {
            code: code,
            nickname: info_res.userInfo.nickName,
            avatar: info_res.userInfo.avatarUrl,
            encryptedData: info_res.encryptedData,
            sex: info_res.userInfo.gender,
            iv: info_res.iv
          },false,'正在登陆').then(res => {
            wx.setStorageSync('user_id', res.user_id);
            app.globalData.user_id = res.user_id;
            // if(res.tel == '' || !res.tel){
            //   wx.navigateTo({
            //     url: '/pages/signin/signin',
            //   })
            // }
            app.globalData.tel = res.tel == null ? '' : res.tel
            app.globalData.openid = res.openid
            wx.setStorageSync('user_info', res.user_info);
            wx.setStorageSync('tel', res.tel == null ? '' : res.tel)
            wx.setStorageSync('openid', res.openid)
            app.globalData.login_status = true
            wx.setStorageSync('login_status', true)
            let page = getCurrentPages()
            let route = page[page.length - 2].route //上一页地址
            wx.navigateBack()
            this.setData({
              loading: false,
              disabled: false
            })
          }).catch(() => {
            this.setData({
              loading: false,
              disabled: false
            })
          })
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none'
          })
          this.setData({
            loading: false,
            disabled: false
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      app_name: wx.getStorageSync('app_name'),
      app_logo: wx.getStorageSync('app_logo'),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})
